﻿/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-19
  File:    DispatchKey.cs  
  Description: 
    Implementation of the key in a process dispatch table pair.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SL.M.Collections
{
    /// <summary>
    /// Represent a process dispatch table key.
    /// </summary>
    public class DispatchKey : IEquatable<DispatchKey>
    {
        /// <summary>
        /// Return or set the source of the dispatch.
        /// </summary>
        /// <remarks>Use one of the static methods of DispatchSource to create this.</remarks>
        public IDispatchSource Source
        {
            get;
            private set;
        }

        /// <summary>
        /// Return or set the message being dispatched.
        /// </summary>
        public MessageType Message
        {
            get;
            private set;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="source">The dispatch sender.</param>
        /// <param name="message">The dispatch message.</param>
        public DispatchKey(IDispatchSource source, MessageType message)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            Source = source;
            Message = message;
        }

        /// <summary>
        /// Return a hash code for this dispatch entry.
        /// </summary>
        /// <returns>An integer.</returns>
        public override int GetHashCode()
        {
            return Message.GetHashCode();
        }

        /// <summary>
        /// Return if the two objects are equal.
        /// </summary>
        /// <param name="obj">The object to compare.</param>
        /// <returns>True if the two objects are equal.</returns>
        public override bool Equals(object obj)
        {
            DispatchKey key = obj as DispatchKey;
            if (key != null)
            {
                return Equals(key);
            }
            return base.Equals(obj);
        }

        /// <summary>
        /// Return if the two keys are equal.
        /// </summary>
        /// <param name="other">The other dispatch key.</param>
        /// <returns>True if the keys are equal.</returns>
        public bool Equals(DispatchKey other)
        {
            if (other != null)
            { 
                if (Message.Equals(other.Message) && Source.Equals(other.Source))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
